package com.qdev.apps.core.webwork.validator;

import java.util.Map;

import com.opensymphony.webwork.validators.ScriptValidationAware;
import com.opensymphony.xwork.validator.validators.RequiredFieldValidator;

/**
 * @author   Quake Wang
 * @since    2005-1-10
 * @version $Revision: 1.1 $
 */
public class JavaScriptRequiredCheckBoxFieldValidator extends RequiredFieldValidator implements ScriptValidationAware {

    public String validationScript(Map parameters) {
        String field = (String) parameters.get("name");
        StringBuffer js = new StringBuffer();
        
        js.append("checkboxs = form.elements['" + field + "'];\n");
        js.append("checked = false;\n");
        js.append("for(i = 0; i < checkboxs.length; i++){\n");
        js.append("\tif(checkboxs[i].checked){\n");
        js.append("\t\tchecked = true;\n");
        js.append("\t\tbreak;\n");
        js.append("\t}\n");
        js.append("}\n");
        
        js.append("if (!checked){\n");
        js.append("\talert('" + getMessage(null) + "');\n");
        js.append("\treturn '" + field + "';\n");
        js.append("}\n");
        js.append("\n");
        
        return js.toString();
    }

}
